/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/09/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.yfstandard.struts.actions;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;
import org.apache.struts.actions.DispatchAction;

import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.yfstandard.commons.Constants;
import com.application.yfstandard.entity.YfStandard;
import com.application.yfstandard.service.YfStandardService;
import com.application.yfstandard.struts.form.YfStandardForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.RandomKey;
import com.manager.commons.page.PageBean;
 /**
 * 
 * Description:YfStandardAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060    <br />
 * 
 * History: <br />
 * 
 * 02/09/2012 
 * 
 */
public class YfStandardAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfStandardService yfStandardService;

	private AttachmentService attachmentService;
	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
        private YfStandard yfStandard;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfStandardPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfStandard" + "开始");
		YfStandardForm yfStandard = (YfStandardForm)form;
		if(yfStandard.getBussType()==null){
			Long busyType = Long.valueOf(request.getParameter("bussType"));
			yfStandard.setBussType(busyType);
		}
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfStandardList = null;
		resultSize = yfStandardService.getAllCountYfStandard(yfStandard);
		pageBean = PageBean.getPageBean("yfStandardTab", resultSize,request);
		yfStandardList = yfStandardService.searchYfStandard(yfStandard,pageBean);
		request.setAttribute("yfStandardList", yfStandardList);

		log.debug("searchYfStandard" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfStandard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfStandards");
		YfStandardForm yfStandardForm = (YfStandardForm) form;
		yfStandardForm.setId(RandomKey.generateID_Long_19());
		yfStandardForm.setRname(CommonUtil.getCurrentUser(request).getRname());
		yfStandardForm.setCreateDate(new Date());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfStandard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfStandards");
		YfStandard yfStandard = this.getYfStandard(form);
		 yfStandard.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfStandard.setCreateDate(new Date());

		this.yfStandardService.insertObject(yfStandard);
		 ActionRedirect redirect = new ActionRedirect(mapping.findForward(Constants.SUCCESS)); 
		 redirect.addParameter("bussType", yfStandard.getBussType());
		    
		return redirect;
	
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfStandard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfStandards");
		String serverPath=CommonUtil.ATTACHE_FILE;
		if(StringUtils.isBlank(serverPath)){
			serverPath=request.getSession().getServletContext().getRealPath("/"); 
		}
		YfStandardForm yfStandardForm = (YfStandardForm) form;
		yfStandard = new YfStandard();
		yfStandard.setId(yfStandardForm.getId());
		yfStandard = yfStandardService.getYfStandard(yfStandard);
		this.yfStandardService.deleteYfStandards(yfStandardForm);
	 
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(yfStandardForm.getId().toString());
		attachmentForm.setType(getType(yfStandardForm.getBussType().intValue()));
		List<Attachment> attachment = attachmentService.queryAttachment(attachmentForm);
		if(attachment!=null && attachment.size()>0){
			for (Attachment attachment2 : attachment) {
				File file = new File(serverPath + attachment2.getUrl());
		        file.delete();
				attachmentService.deleteObject(attachment2);
				
			}
		}
		 ActionRedirect redirect = new ActionRedirect(mapping.findForward(Constants.SUCCESS)); 
		 redirect.addParameter("bussType", yfStandard.getBussType());   
		return redirect;
	}
    private String getType(int bussType){
    	String type = null ;
    	switch(bussType){
    	  case 0:
    		  type = com.manager.commons.Constants.FJ_TYPE_GSZC;
    		  break;
    	  case 1:
    		  type = com.manager.commons.Constants.FJ_TYPE_KFGF;
    		  break;
    	  case 2:
    		  type = com.manager.commons.Constants.FJ_TYPE_ZBGF;
    		  break;
    		 default:
    			 type=null;
    			  
    	}
    	return type;
    }
	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfStandard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfStandards");

		YfStandardForm yfStandardForm = (YfStandardForm) form;
		yfStandard = this.yfStandardService.searchYfStandard(yfStandardForm, null).get(0);
		request.setAttribute(Constants.YFSTANDARD, yfStandard);
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfStandard(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfStandards");
		 YfStandard yfStandard = this.getYfStandard(form);
	     yfStandard.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfStandard.setCreateDate(new Date());
		 this.yfStandardService.updateObject(yfStandard);
		 ActionRedirect redirect = new ActionRedirect(mapping.findForward(Constants.SUCCESS)); 
		 redirect.addParameter("bussType", yfStandard.getBussType());
		return redirect;
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfStandard(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfStandard");

		YfStandardForm yfStandardForm = (YfStandardForm) form;
		yfStandard = this.yfStandardService.searchYfStandard(yfStandardForm, null).get(0);
		request.setAttribute(Constants.YFSTANDARD, yfStandard);
		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the YfStandardService
	 */
	public YfStandardService getYfStandardService() {
		return this.yfStandardService;
	}

	/**
	 * @param YfStandardService
	 *  the YfStandardService to set
	 */
	public void setYfStandardService(YfStandardService yfStandardService) {
		this.yfStandardService = yfStandardService;
	}

	/**
	 * 获取YfStandard实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfStandard
	 */
	public YfStandard getYfStandard(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfStandardForm yfStandardForm = (YfStandardForm) form;
		yfStandard = new YfStandard();
		BeanUtils.copyProperties(yfStandard, yfStandardForm);
		return yfStandard;
	}
    
        public void setYfStandard(YfStandard yfStandard) {
		this.yfStandard = yfStandard;
	}

		public void setAttachmentService(AttachmentService attachmentService) {
			this.attachmentService = attachmentService;
		}

}
